package com.aaa.service.impl;

import com.aaa.dao.NoticeDao;
import com.aaa.dao.UserDao;
import com.aaa.entity.Dept;
import com.aaa.entity.Notice;
import com.aaa.entity.User;
import com.aaa.service.NoticeService;
import com.aaa.vo.NoticeVo;
import com.aaa.vo.Result;
import com.aaa.vo.UserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Objects;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeDao, Notice> implements NoticeService {
    @Autowired
    private NoticeDao noticeDao;
    @Override
    public Result<IPage<Notice>> findByConditions(Integer currentPage, Integer pageSize, NoticeVo noticeVo) {
        IPage<Notice> page=new Page<>(currentPage,pageSize);
        QueryWrapper<Notice> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(noticeVo.getNoticeTitle())){
            wrapper.like("notice_title",noticeVo.getNoticeTitle());
        }
        if(StringUtils.hasText(noticeVo.getCreateBy())){
            wrapper.eq("create_by",noticeVo.getCreateBy());
        }
        if(Objects.nonNull(noticeVo.getNoticeId())){
            wrapper.eq("notice_id",noticeVo.getNoticeId());
        }
        if(Objects.nonNull(noticeVo.getStatus())){
            wrapper.eq("status",noticeVo.getStatus());
        }
        if(Objects.nonNull(noticeVo.getNoticeType())){
            wrapper.eq("notice_type",noticeVo.getNoticeType());
        }
        if(Objects.nonNull(noticeVo.getDateRange())&&noticeVo.getDateRange().length==2){
            wrapper.between("create_time",noticeVo.getDateRange()[0],noticeVo.getDateRange()[1]);
        }

        IPage<Notice> page1 = noticeDao.selectPage(page, wrapper);
        return new Result<IPage<Notice>>(2000,"查询用户成功",page1);
    }

    //添加修改
    @Override
    public boolean saveAndUpdate(Notice notice) {
        int i=-1;
        if (notice.getCreateTime()==null){
            notice.setCreateTime(new Date());
            notice.setUpdateTime(new Date());
        }else {
            notice.setUpdateTime(new Date());
        }
        if(notice.getNoticeId()==null){
            i=noticeDao.insert(notice);
            System.out.println("====="+notice.getStatus());
        }else {
            i=noticeDao.updateById(notice);
        }
        if (i>0){
            return true;
        }
        return false;
    }
    //删除
    @Override
    public boolean delById(Integer id) {
        int i = noticeDao.deleteById(id);
        if (i > 0) {
            return true;
        }
        return false;
    }

}
